package com.mender.product.controller.admin;

import com.mender.domain.po.Setmeal;
import com.mender.product.domain.dto.SetmealDTO;
import com.mender.product.domain.dto.SetmealPageQueryDTO;
import com.mender.product.domain.vo.SetmealVO;
import com.mender.product.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import com.mender.common.result.PageResult;
import com.mender.common.result.Result;

import java.util.List;
import java.util.Map;

/**
 * 菜品管理
 */
@RequestMapping("/admin/setmeal")
@RestController
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping()
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.getCategoryId()")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐信息：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询参数：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        setmealService.delete(ids);
        return Result.success();
    }


    /**
     * 实现修改数据时的回显功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐：{}", id);
        SetmealVO setmealVO = setmealService.getSetmealAnddishById(id);
        return Result.success(setmealVO);
    }
    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "起售停售套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("起售停售套餐：status={},id={}", status,id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }


    /**
     * 根据id查询套餐
     * @param ids
     * @return
     */
    @GetMapping("/selectByIds")
    @ApiOperation(value = "根据条件查询套餐")
    public Result<List<Setmeal>> selectByIds(@RequestParam List<Long> ids) {
        log.info("根据条件查询套餐：{}", ids);
        List<Setmeal> setmeals = setmealService.selectByIds(ids);
        return Result.success(setmeals);
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @PostMapping("/countSetmealByMap")
    @ApiOperation(value = "查询套餐总览")
    public Result<Integer> countSetmealByMap(@RequestBody Map<String, Object> params) {
        log.info("查询套餐总览：");
        Integer setmeals = setmealService.countSetmealByMap(params);
        return Result.success(setmeals);
    }
}
